<?php

/**
 * Single FieldForm
 */
class FieldForm extends CFormModel
{
	public $name;
	public $type = 'string';
	public $index = 'none';
	public $tokenizer = 'default';
	public $cutlen = 0;
	public $weight = 1;
	public $phrase;

	public function rules()
	{
		return array(
			array('name', 'required'),
			//array('type', 'range', 'range' => array_keys($this->typeOptions())),
			array('type, index, tokenizer, cutlen, weight, phrase', 'safe'),
		);
	}

	/**
	 * @param XSFieldMeta $field
	 */
	public function fromFieldMeta($field)
	{
		$this->name = $field->name;
	}

	public function attributeLabels()
	{
		return array(
			'name' => '字段名称',
			'type' => '字段类型',
			'index' => '索引方式',
			'tokenizer' => '分词器',
			'cutlen' => '截断长度',
			'weight' => '权重值',
			'phrase' => '精确搜索',
		);
	}

	public function typeOptions()
	{
		return array(
			'string' => '字符型',
			'numeric' => '数字型',
			'date' => '日期型',
			'id' => '主键型',
			'title' => '标题型',
			'body' => '内容型',
		);
	}

	public function indexOptions()
	{
		return array(
			'none' => '不索引',
			'self' => '字段索引',
			'mixed' => '混合区索引',
			'both' => '字段和混合区索引',
		);
	}

	public function tokenizerOptions()
	{
		$options = array(
			'default' => 'default',
			'full' => 'full',
			'split' => 'split',
			'xlen' => 'xlen',
			'xstep' => 'xstep',
			'none' => 'none',
		);
		if (!isset($options[$this->tokenizer])) {
			$options[$this->tokenizer] = 'CUSTOM:' . $this->tokenizer;
		}
		return $options;
	}

	public function phraseOptions()
	{
		return array(
			'no' => '否',
			'yes' => '是',
		);
	}
}
